package com.itheima.controller.admin;


import com.itheima.dto.OrdersPageQueryDTO;
import com.itheima.dto.OrdersStatisticsDto;
import com.itheima.pojo.Orders;
import com.itheima.service.OrderService;
import com.itheima.vo.*;
import com.itheima.result.PageBean;
import com.itheima.result.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/admin/order")
@Api(tags = "订单相关接口")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 订单分页查询
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping("/conditionSearch")
    public Result find(OrdersPageQueryDTO ordersPageQueryDTO){
        PageBean pageBean= orderService.page(ordersPageQueryDTO);
        return Result.success(pageBean);

    }


    /**
     * 后端根据id查询订单
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    public Result findByIdOrderWithOrderDetail(@PathVariable String id){
        OrderVO orderVO = orderService.findById(Long.valueOf(id));
        return Result.success(orderVO);
    }

    /**
     * 接单
     * @param orders
     * @return
     */
    @PutMapping("/confirm")
    public Result confirmOrder(@RequestBody Orders orders){
        orderService.confirmOrder(orders);
        return Result.success();
    }

    /**
     * 派送订单
     * @param id
     * @return
     */
    @PutMapping("/delivery/{id}")
    public Result deliveryById(@PathVariable Long id){
        orderService.deliveryById(id);
        return Result.success();
    }

    /**
     * 拒绝接单
     * @param orders
     * @return
     */
    @PutMapping("/rejection")
    public Result reject(@RequestBody Orders orders){
        orderService.reject(orders);
        return Result.success();
    }

    /**
     * 取消订单
     * @param orders
     * @return
     */
    @PutMapping("/cancel")
    public Result cancel(@RequestBody Orders orders){
        orderService.cancel(orders);
        return Result.success();
    }

    /**
     * 完成订单
     * @param id
     * @return
     */
    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable Long id){
        orderService.complete(id);
        return Result.success();
    }

    /**
     * 各个状态的订单数量统计
     * @return
     */
    @GetMapping("/statistics")
    public Result getCount(){
        OrderStatisticsVO orderStatisticsVO=orderService.getCount();
        return Result.success(orderStatisticsVO);
    }
    @GetMapping("/ordersStatistics")
    public Result<OrderReportVO> orderStatistics(OrdersStatisticsDto statisticsDto) {
        log.info("statisticsDto为{}", statisticsDto);
        OrderReportVO vo = orderService.orderStatistics(statisticsDto);
        return Result.success(vo);
    }
    @GetMapping("/top10")
    public Result<SalesTop10ReportVO> top(OrdersStatisticsDto dto){
        log.info("dto为{}", dto);
        SalesTop10ReportVO vo=orderService.top(dto);
        return Result.success(vo);
    }

    @GetMapping("/turnoverStatistics")
    public TurnoverReportVO turnoverStatistics(OrdersStatisticsDto statisticsDto) {
        TurnoverReportVO vo=orderService.turnoverStatistics(statisticsDto);
        return vo;
    }

    @GetMapping("/getBusinessData")
    public BusinessDataVO getBusinessData(LocalDateTime begin,LocalDateTime end){
        return orderService.getBusinessData(begin,end);
    }

    @GetMapping("/getData")
    public List<BusinessDataVO> getData(LocalDate begin,LocalDate end){
       return orderService.getData(begin,end);
    }

    @GetMapping("/overviewOrders")
    public OrderOverViewVO overviewOrders() {

        return orderService.overviewOrders();
    }

    @GetMapping("/businessData")
    public BusinessDataVO businessData(){
        return orderService.businessData();
    }
}
